package com.fluxtah.jordl.examples.twitter;

import com.fluxtah.jordl.lib.TransformerProvider;
import com.fluxtah.jordl.lib.TransformException;
import com.fluxtah.jordl.lib.WebResponse;
import java.io.InputStream;
import org.json.JSONObject;
import com.fluxtah.jordl.lib.StreamHelper;
import java.io.IOException;

public class GetStatusResponse  {
	private Tweet tweet;
	public Tweet getTweet(){
		return this.tweet;
	}
	public GetStatusResponse(TransformerProvider transformerProvider, WebResponse response) throws TransformException {
		InputStream stream = response.getContent();
		try {
			if(stream != null && stream.available() > 0){
				String content = StreamHelper.streamToString(stream);
				JSONObject source = new JSONObject(content);
				this.tweet = new Tweet();
				transformerProvider.get(TweetInputTransformer.class).transform(source, this.tweet);
			}
		} catch(Exception x) {
			throw new TransformException(x);
		} finally {
			if(stream != null) {
				try {
					stream.close();
				}
				catch (IOException x) {
					throw new TransformException(x);
				}
			}
		}
	}
}
